package com.example.demo.controller;

class Solution {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        reverseList(head);
    }
    public static ListNode reverseList(ListNode head) {
        if(head==null || head.next == null){
            return head;
        }


        ListNode prev = head;
        ListNode cur = prev.next;
        ListNode next = cur.next;
        while(cur != null){
            cur.next = prev;

            prev = cur;
            cur = next;
            if(next != null){
                next = next.next;
            }

        }
        return cur;
    }
}
